package com.ggj2013.hbh;

import com.eclecticdesignstudio.spritesheet.AnimatedSprite;
import com.eclecticdesignstudio.spritesheet.importers.SpriteLoq;
//import com.eclecticdesignstudio.spritesheet.importers.AnimoImporter;
import nme.display.Bitmap;
import nme.display.MovieClip;
import com.ggj2013.hbh.utils.Tools;
import com.ggj2013.hbh.utils.HitBox;

/**
 * @author Jonathan Moriarty
 */
class Player extends MovieClip {

	public var currentSprite:AnimatedSprite;
	public var hitBox:HitBox;
	
	public static var SPEED:Float = 4.0;
	
	public static var HITBOX_SIZE:Float = 25.0;
	
	public static var MAX_SANITY:Float = 100.0;
	public static var MIN_SANITY:Float = 0.0;
	public static var SANITY_BURN_RATE:Float = -0.1;
	
	public var currentSanity:Float;
	
	public function new( l_newX:Float, l_newY:Float ) {
		super();
		
		x = l_newX;
		y = l_newY;
		
		var playerSpriteSheet = Tools.loadSpriteSheet( "player" );
		currentSprite = new AnimatedSprite( playerSpriteSheet );
		currentSprite.showBehavior( "move_down" );
		currentSprite.update( 1 );
		currentSprite.x -= currentSprite.width * 0.5 - 1;// + 5;
		currentSprite.y -= currentSprite.height * 0.5 - 5;// + 10;
		currentSprite.scaleX = currentSprite.scaleY = 2;
		addChild( currentSprite );
		
		hitBox = new HitBox( HITBOX_SIZE, HITBOX_SIZE, 0xff0000 );
		addChild( hitBox );
		
		currentSanity = Player.MAX_SANITY;
	}
	
	public function update( l_deltaTime:Int ) {
		currentSprite.update(l_deltaTime);
		currentSanity += Player.SANITY_BURN_RATE;
		currentSanity = Math.max( currentSanity, Player.MIN_SANITY );
		currentSanity = Math.min( currentSanity, Player.MAX_SANITY );
		//trace(currentSanity);
	}
	
	public function setAnimation(_newAnimation:String):Void
	{
		currentSprite.showBehavior(_newAnimation);
	}
	
	public function getAnimation():String
	{
		return currentSprite.currentBehavior.name;
	}
	
	public function stopMovement():Void
	{
		if (currentSprite.currentBehavior.name.indexOf("up") != -1)
		{
			currentSprite.showBehavior("idle_up");
		}
		
		else if (currentSprite.currentBehavior.name.indexOf("down") != -1)
		{
			currentSprite.showBehavior("idle_down");
		}
		
		else if (currentSprite.currentBehavior.name.indexOf("left") != -1)
		{
			currentSprite.showBehavior("idle_left");
		}
		
		else if (currentSprite.currentBehavior.name.indexOf("right") != -1)
		{
			currentSprite.showBehavior("idle_right");
		}
	}

}